<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Admin\Order;
use App\Models\Admin\GoodPrice;
use Maatwebsite\Excel\Facades\Excel;
use App\Imports\ReviewOrderImport;
use DB, Auth, Config;

class OrderController extends Controller
{
    public function getList(Request $request)
    {
        $pageSize = $request['pageSize'];

        $orderId = $request['orderId'];
        $orderCode = $request['orderCode'];
        $orderStatus = $request['orderStatus'];
        $storeId = $request['storeId'];
        $acceptUser = $request['acceptUser'];
        $userId = $request['userId'];

        $report = $request['report'];

        $orderModel = new Order;
        $data = $orderModel->getList($pageSize, $orderId, $orderCode, $orderStatus, $userId, $storeId, $acceptUser, $report);

        return $data;
    }

    public function handle(Request $request)
    {
        $orderId = $request['id'];
        $data = $request['data'];

        $orderModel = new Order;
        $data = $orderModel->handle($orderId, $data);

        return $data;
    }
    //待发货订单列表
    public function getSendList(Request $request)
    {
        $pageSize = $request['pageSize'];

        $orderId = $request['orderId'];
        $orderCode = $request['orderCode'];
        $storeId = $request['storeId'];
        $acceptUser = $request['acceptUser'];
        $userId = $request['userId'];

        $report = $request['report'];

        $orderModel = new Order;
        $data = $orderModel->getSendList($pageSize, $orderId, $orderCode, $userId, $storeId, $acceptUser, $report);

        return $data;
    }

    //发货审核
    public function reviewOrder(Request $request){
        if(!$request->hasFile('file')){
            exit('上传文件为空！');
        }
        $import = new ReviewOrderImport;
        $result = Excel::import($import, $request->file('file'));
        return $this->success(); 
    }

    public function reviewOrderList(Request $request){
        $order = new Order;
        $path = env('APP_URL').'/order/review_order';
        $data = $order->reviewOrder($request,$path);
        return $data;
    }

    public function reviewOrderGold(Request $request){
        $order = new Order;
        $data = $order->reviewOrder($request);
        if($data['code'] != 20000){
            return $data;
        }
        $orderitem = $data['data']['data'];
        $goods = $data['data']['statistics'];
        $goodIds = array_column($goods,'good_id');
        $goodsPrice = GoodPrice::whereIn('good_id',$goodIds)->get()->toArray();
        $info = [];
        foreach ($orderitem as $key => $value) {
            foreach ($goodsPrice as $k => $v) {
                if($value['good_id'] == $v['good_id']){
                    $orderitem[$key]['price'] = $v['price'];
                }
            }
            if(!isset($info[$value['order_id']])){
                $info[$value['order_id']]['store_id'] = $value['store_id'];
                $info[$value['order_id']]['store_title'] = $value['store_title'];
                $info[$value['order_id']]['order_id'] = $value['order_id'];
                $info[$value['order_id']]['user_id'] = $value['user_id'];
                $info[$value['order_id']]['type_cn'] = Config::get('const.order_type_str')[$value['type']];
                $info[$value['order_id']]['price'] = (int)($orderitem[$key]['price'] * $value['goods_quantity'])/100;
                $info[$value['order_id']]['pay_price'] = (int)($orderitem[$key]['price'] * $value['goods_quantity'] - $value['jf_price'])/100;//(int)($value['pay_price'] - $value['jf_price'])/100;
                $info[$value['order_id']]['jf_price'] = (int)$value['jf_price']/100;
                $info[$value['order_id']]['pay_at'] = $value['pay_at'];
            }else{
                $info[$value['order_id']]['price'] += (int)($orderitem[$key]['price'] * $value['goods_quantity'])/100;
                $info[$value['order_id']]['pay_price'] += (int)($orderitem[$key]['price'] * $value['goods_quantity'])/100;
            }
        }

        $list = [];
        foreach ($info as $key => $value) {
            array_push($list,$value);
        }
        $arr = [];
        $arr['pay_price'] = (int)array_sum(array_column($list,'pay_price'));
        $arr['jf_price'] = (int)array_sum(array_column($list,'jf_price'));
        $arr['price'] = (int)array_sum(array_column($list,'price'));
        $arr['count'] = count($list);
        $arr['data'] = $list;
        return $this->success($arr);
    }
}
